package com.extremelylargepackage.editor;

import java.util.ArrayList;

import com.extremelylargepackage.R;
import com.extremelylargepackage.entity.Enemy;
import com.extremelylargepackage.level.Wave;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;

public class WaveEditor extends ActivityForcePortrait 
{
	private Wave _wave;
	private int _num;
	private ListView _list;
	private ArrayAdapter<LaneListItem> _adapter;
	private EditText _duration;
	private CheckBox _durationEnable;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wave_editor);
		_list = (ListView) findViewById(R.id.laneList);
		_wave = (Wave) getIntent().getExtras().get("wave");
		_num = getIntent().getExtras().getInt("num");
		_list.setOnItemClickListener(new WaveHandler());
		
		Button ret = (Button) findViewById(R.id.wave_editor_return);
		ret.setOnClickListener(new ReturnHandler());
		
		_duration = (EditText) findViewById(R.id.wave_duration);
		_durationEnable = (CheckBox) findViewById(R.id.wave_duration_bool);
		_durationEnable.setChecked(_wave.getDuration() > 0);
		_duration.setText("" + _wave.getDuration());
		_durationEnable.setOnClickListener(new DurationEnableHandler());
		
		_duration.setEnabled(_durationEnable.isChecked());
		constructList();
	}
	
	public void constructList()
	{
		ArrayList<LaneListItem> array = new ArrayList<LaneListItem>();
		for(int i = 0; i < 8; i++)
		{
			LaneListItem lli = new LaneListItem(i+1, _wave.getEnemies().get(i+1));
			array.add(lli);
		}
		_adapter = new ArrayAdapter<LaneListItem>(this, android.R.layout.simple_list_item_1, array);
		_list.setAdapter(_adapter);
	}
	
	@Override
	public void onBackPressed()
	{
		setResultAndFinish();
	}
	
	private void setResultAndFinish()
	{
		Intent i = new Intent();
		if(_durationEnable.isChecked())
			_wave.setDuration(Long.parseLong(_duration.getText().toString()));
		else
			_wave.setDuration(-1);
		i.putExtra("wave", _wave);
		i.putExtra("num", _num);
		setResult(Activity.RESULT_OK, i);
		finish();
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		switch(resultCode)
		{
			case Activity.RESULT_OK:
				int row = data.getExtras().getInt("num");
				Enemy e = (Enemy) data.getExtras().get("enemy");
				((LaneListItem)_list.getItemAtPosition(row - 1)).setEnemy(e);
				_adapter.notifyDataSetChanged();
				break;
			default:
				break;
		}
	}
	
	private class WaveHandler implements OnItemClickListener
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View v, int row, long id) 
		{
			((LaneListItem) _list.getItemAtPosition(row)).edit();
		}
	}
	
	private class ReturnHandler implements OnClickListener
	{
		@Override
		public void onClick(View v) 
		{
			setResultAndFinish();
		}
	}
	
	private class DurationEnableHandler implements OnClickListener
	{
		@Override
		public void onClick(View v) 
		{
			//_durationEnable.setChecked(!_durationEnable.isChecked());
			_duration.setEnabled(_durationEnable.isChecked());
		}
	}
	
	private class LaneListItem
	{
		private int _num;
		private Enemy _enemy;
		
		public LaneListItem(int num, Enemy e)
		{
			_num = num;
			_enemy = e;
		}
		
		public void edit()
		{
			Intent i = new Intent(WaveEditor.this, EnemySelect.class);
			i.putExtra("enemy", _enemy);
			i.putExtra("num", _num);
			startActivityForResult(i, 0);
		}
		
		public void setEnemy(Enemy e)
		{
			_enemy = e;
			if(e == null)
				_wave.getEnemies().remove(_num);
			else
				_wave.getEnemies().put(_num, e);
		}
		
		public Enemy getEnemy()
		{
			return _enemy;
		}
		
		@Override
		public String toString()
		{
			return  "Lane " + _num + " - " + (_enemy != null ? _enemy.getName() : "NONE");
		}
	}
}
